
#import "RIBMakeTexture.h"
#import "ri/ri.h"

@implementation RIBMakeTexture

+ initialize { return [RIBMakeTexture setVersion:1], self; }

- init
{
  [super init];

  pictureName = textureName = NULL;
  sWrap = tWrap = RI_CLAMP;
  filterFunc = (RtFloatFunc)RiGaussianFilter;
  filterFuncName = NXCopyStringBuffer("gaussian");
  sWidth = tWidth = 3.0;

  return self;
}

- setPictureName:(const char *)newPictureName textureName:(const char *)newTextureName
     sWrap:(RtToken)newSWrap tWrap:(RtToken)newTWrap filterFuncName:(const char *)newFilterFuncName
     sWidth:(RtFloat)newSWidth tWidth:(RtFloat)newTWidth 
{
   pictureName = NXCopyStringBuffer(newPictureName);
   textureName = NXCopyStringBuffer(newTextureName);
   sWrap = NXCopyStringBuffer(newSWrap);
   tWrap = NXCopyStringBuffer(newTWrap);
   if (!strcmp(newFilterFuncName, "box"))
   {  filterFunc = (RtFloatFunc)RiBoxFilter; 
      filterFuncName = NXCopyStringBuffer("box"); 
   }
   if (!strcmp(newFilterFuncName, "triangle"))
   {  filterFunc = (RtFloatFunc)RiTriangleFilter; 
      filterFuncName = NXCopyStringBuffer("triangle"); 
   }
   if (!strcmp(newFilterFuncName, "catmull-rom"))
   {  filterFunc = (RtFloatFunc)RiCatmullRomFilter; 
      filterFuncName = NXCopyStringBuffer("catmull-rom"); 
   }
   if (!strcmp(newFilterFuncName, "sinc"))
   {  filterFunc = (RtFloatFunc)RiSincFilter; 
      filterFuncName = NXCopyStringBuffer("sinc"); 
   }
   if (!strcmp(newFilterFuncName, "gaussian"))
   {  filterFunc = (RtFloatFunc)RiGaussianFilter; 
      filterFuncName = NXCopyStringBuffer("gaussian"); 
   }
   sWidth = newSWidth;
   tWidth = newTWidth;

   return self;
}

- renderMaps:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime usingStream:(NXStream *)ns
{
   NXPrintf(ns, 
	    "MakeTexture \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %f %f ", 
	    pictureName, textureName, sWrap, tWrap, filterFuncName, sWidth, tWidth);
   //[super writeParameterList:ns];
   return self;
}

- renderMaps:(WW3DCamera *)camera usingStream:(NXStream *)ns
{
  RtFloat  shutterOpenTime = [camera shutterOpenTime],
           shutterCloseTime = [camera shutterCloseTime];


  return [self renderMaps:camera startingAt:shutterOpenTime endingAt:shutterCloseTime usingStream:ns];
}


- renderMaps:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiMakeTexture(pictureName, textureName, sWrap, tWrap, filterFunc, sWidth, tWidth, RI_NULL);

  return self;
}

- renderMaps:(WW3DCamera *)camera
{
  RtFloat  shutterOpenTime = [camera shutterOpenTime],
           shutterCloseTime = [camera shutterCloseTime];


  return [self renderSelf:camera startingAt:shutterOpenTime endingAt:shutterCloseTime];
}


- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, 
	    "MakeTexture {%s} {%s} {%s} {%s} {%s} %f %f ", 
	    pictureName, textureName, sWrap, tWrap, filterFuncName, sWidth, tWidth);
   [super writeParameterList:stream];
   return self;
}

#define typeVector "*****ff"
#define typeValues &pictureName, &textureName, &sWrap, &tWrap, &filterFuncName, &sWidth, &tWidth

- read:(NXTypedStream *)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream, "RIBMakeTexture");
    if (version == 0) NXReadTypes(stream, "i", &version), version=1;
    if (version == 1)
    {  NXReadTypes(stream, typeVector, typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream *)stream 
{
    [super write:stream];

    NXWriteTypes(stream, typeVector, typeValues);

    return self;
}
@end
